package cn.gson.crm.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gson.crm.model.dao.LLJ_CustomRecordDao;
import cn.gson.crm.model.dao.LLJ_ServiceTypeDao;
import cn.gson.crm.model.dao.LLJ_ServiceWayDao;
import cn.gson.crm.model.dao.LLJ_TimeDealDao;
import cn.gson.crm.model.pojo.ComplainType;
import cn.gson.crm.model.pojo.CustomRecord;
import cn.gson.crm.model.pojo.ServiceType;
import cn.gson.crm.model.pojo.ServiceWay;
import cn.gson.crm.model.pojo.TimeDeal;
import cn.gson.crm.model.pojoVo.LLJ_ComplainTypeVo;
import cn.gson.crm.model.pojoVo.LLJ_CustomRecordVo;
import sun.security.krb5.internal.crypto.crc32;

@Service
@Transactional(rollbackFor = Exception.class)
public class LLJ_customerService {
	@Autowired
	private LLJ_CustomRecordDao CustomRecordDao; 
	@Autowired
	private LLJ_ServiceTypeDao llj_ServiceTypeDao;
	@Autowired
	private LLJ_ServiceWayDao llj_ServiceWayDao;
	@Autowired LLJ_TimeDealDao llj_TimeDealDao;
  
	/**
	 * 查询客户服务信息  
	 * @throws Exception 
	 */
    public List<LLJ_CustomRecordVo> findAllCustomerMessage() throws Exception{
    	   List<LLJ_CustomRecordVo> listVo=new ArrayList<>();
    	   
    	  try {   
    		  List<CustomRecord> customRecordList= (List<CustomRecord>) CustomRecordDao.findAll();
       	   for(CustomRecord c:customRecordList){
       		   LLJ_CustomRecordVo Vo=new LLJ_CustomRecordVo();
       		   Vo.setCrId(c.getCrId());    
       		   Vo.setContId(c.getContId());
       		   Vo.setContLinkMan(c.getContLinkMan());
       		   Vo.setCrContain(c.getCrContain());
       		   Vo.setCrExcute(c.getCrExcute());
       		   Vo.setCrFeedback(c.getCrFeedback());
       		   Vo.setCrRemarks(c.getCrRemarks());
       		   Vo.setCrStartDatetime(c.getCrStartDatetime());
       		   Vo.setCrState(c.getCrState());
       		   Vo.setCrTheme(c.getCrTheme());
       		   Vo.setCustomerId(c.getCustomerId());
       		   Vo.setCustomerName(c.getCustomerName());
       		   
       		   ServiceType sT=c.getServiceType();
       		   ServiceWay sW=c.getServiceWay();
       		   TimeDeal tD=c.getTimeDeal();
       		   if(sT!=null){
       			 Vo.setServiceId(sT.getServiceId());
         		 Vo.setServiceContain(sT.getServiceContain()); 
       		   } 
       		   if(sW!=null){
       			 Vo.setServiceWayId(c.getServiceWay().getServiceWayId());
        		 Vo.setServiceWayContain(c.getServiceWay().getServiceWayContain());  
       		   }
       		   if(tD!=null){  
       			 Vo.setDealId(c.getTimeDeal().getDealId());
        		 Vo.setDealContain(c.getTimeDeal().getDealContain());
       		   }
       		   listVo.add(Vo);
       	   }
       	return listVo;  
		} catch (Exception e) {
			throw new Exception(e); 
		}  
    }
	
    /**
     * 添加客户服务记录
     * @throws Exception 
     */
    public boolean addCustomerService(LLJ_CustomRecordVo Vo) throws Exception{
    	boolean jugde=false;
    	try {
    		CustomRecord cRecord=new CustomRecord();
        	cRecord.setContLinkMan(Vo.getContLinkMan());
        	cRecord.setCrContain(Vo.getCrContain());
        	cRecord.setCrExcute(Vo.getCrExcute());
        	cRecord.setCrFeedback(Vo.getCrFeedback());
        	cRecord.setCrRemarks(Vo.getCrRemarks());
        	cRecord.setCrStartDatetime(Vo.getCrStartDatetime());
        	cRecord.setCrState(Vo.getCrState());
        	cRecord.setCrTheme(Vo.getCrTheme());
        	cRecord.setCustomerId(Vo.getCustomerId());
        	cRecord.setCustomerName(Vo.getCustomerName());
        	    
        	Long timeId=(long) Integer.parseInt(Vo.getDealContain());
        	cRecord.setServiceType(llj_ServiceTypeDao.findOne(Vo.getServiceId()));
        	cRecord.setServiceWay(llj_ServiceWayDao.findOne(Vo.getServiceWayId()));
        	cRecord.setTimeDeal(llj_TimeDealDao.findOne(timeId));
        	  
        	CustomRecordDao.save(cRecord); 
        	jugde=true;
		} catch (Exception e) { 
			throw new Exception(e); 
		}  
    	return jugde;  
    }
    
    /**
     * 初始化客服服务信息  
     */
    public LLJ_CustomRecordVo initedMessage(Long crId){
    	CustomRecord c=CustomRecordDao.findOne(crId);
    	LLJ_CustomRecordVo CrecordVo=new LLJ_CustomRecordVo();
    	CrecordVo.setCrId(c.getCrId());
    	CrecordVo.setContId(new BigDecimal(1));   
    	CrecordVo.setContLinkMan(c.getContLinkMan());
    	CrecordVo.setCrContain(c.getCrContain());  
    	CrecordVo.setCrExcute(c.getCrExcute());
    	CrecordVo.setCrFeedback(c.getCrFeedback());
    	CrecordVo.setCrRemarks(c.getCrRemarks());
    	CrecordVo.setCrState(c.getCrState());
    	CrecordVo.setCrTheme(c.getCrTheme());  
    	CrecordVo.setCustomerId(c.getCustomerId());
    	CrecordVo.setCustomerName(c.getCustomerName());
    	CrecordVo.setCrStartDatetime(c.getCrStartDatetime());
    	
    	ServiceType sT=c.getServiceType();
		ServiceWay sW=c.getServiceWay();
		TimeDeal tD=c.getTimeDeal();
		if(sT!=null){
		   CrecordVo.setServiceId(sT.getServiceId());
		   CrecordVo.setServiceContain(sT.getServiceContain());
		}
		if(sW!=null){
			CrecordVo.setServiceWayId(sW.getServiceWayId());
			CrecordVo.setServiceWayContain(sW.getServiceWayContain());
		}
		if(tD!=null){
			CrecordVo.setDealId(tD.getDealId());
			CrecordVo.setDealContain(tD.getDealContain());
		}   
    	return CrecordVo;     
    }
    
    /**
     * 编辑客服服务信息
     * @throws Exception    
     */
    public boolean editCustomerMessage(LLJ_CustomRecordVo Cvo) throws Exception{
    	boolean judge=false;
    	try {
    		CustomRecord c=new CustomRecord();
        	//c.setContId(Cvo.getContId());
        	c.setContLinkMan(Cvo.getContLinkMan());
        	c.setCrContain(Cvo.getCrContain());
        	c.setCrExcute(Cvo.getCrExcute());
        	c.setCrFeedback(Cvo.getCrFeedback());
        	c.setCrRemarks(Cvo.getCrRemarks());
        	c.setCrStartDatetime(Cvo.getCrStartDatetime());
        	c.setCrState(Cvo.getCrState());
        	c.setCrTheme(Cvo.getCrTheme());
        	//c.setCustomerId(Cvo.getCustomerId());
        	c.setCustomerName(Cvo.getCustomerName());
        	
        	Long timeId=(long) Integer.parseInt(Cvo.getDealContain());
        	c.setServiceType(llj_ServiceTypeDao.findOne(Cvo.getServiceId()));   
        	c.setServiceWay(llj_ServiceWayDao.findOne(Cvo.getServiceWayId()));
        	c.setTimeDeal(llj_TimeDealDao.findOne(timeId)); 
        	
        	CustomRecordDao.findOne(Cvo.getCrId());  
        	CustomRecordDao.save(c);    
    		return true;
		} catch (Exception e) {  
			throw new Exception(e);  
		}
    }
    
    /**
     * 删除一条客服记录
     * @throws Exception 
     */
    public boolean deleteOneData(Long crId) throws Exception{
    	boolean judge=false;
    	try {
    		CustomRecord customRecord=CustomRecordDao.findOne(crId);
        	CustomRecordDao.delete(customRecord);
        	judge=true;
		} catch (Exception e) {
		    throw new Exception(e);
		}
    	return false;    
    }
    
    /**
     * 更具客服服务主题查询
     */
    public List<LLJ_CustomRecordVo> searchDataByTheme(String key){
    	
    	//llj_ServiceTypeDao.findByCrTheme();  
    	return null;  
    }
          
    /**
     * 查询服务类型
     */
    public List<ServiceType> findType(){    
    	return llj_ServiceTypeDao.searchAllType(); 
    }  
    
    /**
     * 查询服务方式
     */
    public List<ServiceWay> findWay(){
    	
    	return llj_ServiceWayDao.searchAllWay();      
    	
    }
}
